/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.check.checkstandardcycle.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.check.checkstandardcycle.entity.CheckStandardCycle;
import com.jeeplus.modules.check.checkstandardcycle.mapper.CheckStandardCycleMapper;
import com.jeeplus.modules.check.checkstandardcycle.entity.CheckStandArdCycleChild;
import com.jeeplus.modules.check.checkstandardcycle.mapper.CheckStandArdCycleChildMapper;

/**
 * 周期点检主附表Service
 * @author 刘明星
 * @version 2020-11-11
 */
@Service
@Transactional(readOnly = true)
public class CheckStandardCycleService extends CrudService<CheckStandardCycleMapper, CheckStandardCycle> {

	@Autowired
	private CheckStandArdCycleChildMapper checkStandArdCycleChildMapper;

	public CheckStandardCycle get(String id) {
		CheckStandardCycle checkStandardCycle = super.get(id);
		checkStandardCycle.setCheckStandArdCycleChildList(checkStandArdCycleChildMapper.findList(new CheckStandArdCycleChild(checkStandardCycle)));
		return checkStandardCycle;
	}

	public List<CheckStandardCycle> findList(CheckStandardCycle checkStandardCycle) {
		return super.findList(checkStandardCycle);
	}

	public Page<CheckStandardCycle> findPage(Page<CheckStandardCycle> page, CheckStandardCycle checkStandardCycle) {
		return super.findPage(page, checkStandardCycle);
	}

	@Transactional(readOnly = false)
	public void save(CheckStandardCycle checkStandardCycle) {
		super.save(checkStandardCycle);
		for (CheckStandArdCycleChild checkStandArdCycleChild : checkStandardCycle.getCheckStandArdCycleChildList()){
			if (checkStandArdCycleChild.getId() == null){
				continue;
			}
			if (CheckStandArdCycleChild.DEL_FLAG_NORMAL.equals(checkStandArdCycleChild.getDelFlag())){
				if (StringUtils.isBlank(checkStandArdCycleChild.getId())){
					checkStandArdCycleChild.setCheckStandard(checkStandardCycle);
					checkStandArdCycleChild.preInsert();
					checkStandArdCycleChildMapper.insert(checkStandArdCycleChild);
				}else{
					checkStandArdCycleChild.preUpdate();
					checkStandArdCycleChildMapper.update(checkStandArdCycleChild);
				}
			}else{
				checkStandArdCycleChildMapper.delete(checkStandArdCycleChild);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(CheckStandardCycle checkStandardCycle) {
		super.delete(checkStandardCycle);
		checkStandArdCycleChildMapper.delete(new CheckStandArdCycleChild(checkStandardCycle));
	}

}